﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Common.Logging;

/// <summary>
/// ConvertUtil 的摘要描述
/// </summary>
public class ConvertUtil
{
    public ConvertUtil()
    {
    }

    /// <summary>
    /// 取得中文的星期幾
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string GetChineseDayOfWeek(DateTime dateTime)
    {
        string result;
        result = System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames[(byte)dateTime.DayOfWeek];
        return result.Replace("星期", string.Empty);
    }

    /// <summary>
    /// 將日期轉為yyyy/mm/dd的字串
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static string DateToShortDateStr(DateTime? date)
    {
        return DateToShortDateStr(date, "/");
    }

    /// <summary>
    /// 將日期轉為yyyy/mm/dd的字串
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static string DateToShortDateStr(DateTime? date, string separator)
    {
        if (date == null)
        {
            return "";
        }
        else
        {
            return (string.Format(date.Value.ToString("yyyy{0}MM{1}dd"), separator, separator));
        }
    }

    /// <summary>
    /// 將yyyy/mm/dd轉換為Datetime格式
    /// </summary>
    /// <param name="dateTimeStr"></param>
    /// <returns></returns>
    public static DateTime? ToDateTime(string dateTimeStr)
    {
        if (string.IsNullOrEmpty(dateTimeStr))
        {
            return null;
        }

        return ToDateTime(dateTimeStr, '/');
    }

    /// <summary>
    /// 將yyyy/mm/dd轉換為Datetime格式
    /// </summary>
    /// <param name="dateTimeStr"></param>
    /// <returns></returns>
    public static DateTime ToDateTime(string dateTimeStr, char separator)
    {
        string[] strArray = dateTimeStr.Split(separator);

        DateTime d = new DateTime(Convert.ToInt32(strArray[0]), Convert.ToInt32(strArray[1]), Convert.ToInt32(strArray[2]));
        return d;
    }

    public static int ToInt32(string p)
    {
        if (!string.IsNullOrEmpty(p) && IsNumber(p))
        {
            return Convert.ToInt32(p);
        }
        else
        {
            return 0;
        }
    }

    public static bool IsNumber(string strNumber)
    {
        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^-?\d+(\.)?\d*$");
        return r.IsMatch(strNumber);
    }

    /// <summary>
    /// 傳回當天日期最後的時間例如1月1日23:59:59
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime ToDateTimeMax(DateTime dateTime)
    {
        DateTime tmp = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59);
        return tmp;
    }

    /// <summary>
    /// 傳回當天日期最早的時間例如1月1日00:00:00
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime ToDateTimeMin(DateTime dateTime)
    {
        DateTime tmp = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0);
        return tmp;
    }

    /// <summary>
    /// 傳回當天日期最後的時間例如1月1日23:59:59
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime? ToDateTimeMax(string dateTimeStr)
    {
        DateTime? dateTime = ToDateTime(dateTimeStr);

        if (dateTime == null)
        {
            return null;
        }

        DateTime tmp = new DateTime(dateTime.Value.Year, dateTime.Value.Month, dateTime.Value.Day, 23, 59, 59);
        return tmp;
    }

    /// <summary>
    /// 傳回當天日期最早的時間例如1月1日00:00:00
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime? ToDateTimeMin(string dateTimeStr)
    {
        DateTime? dateTime = ToDateTime(dateTimeStr);

        if (dateTime == null)
        {
            return null;
        }

        DateTime tmp = new DateTime(dateTime.Value.Year, dateTime.Value.Month, dateTime.Value.Day, 0, 0, 0);
        return tmp;
    }

    /// <summary>
    /// 取得字串的Byte長度
    /// </summary>
    /// <param name="titleLength"></param>
    /// <returns></returns>
    public static int GetStringBytes(string titleLength)
    {
        int length = 0;

        if (!string.IsNullOrEmpty(titleLength.Trim()))
        {
            length = Encoding.Default.GetBytes(titleLength).Length;
        }

        return length;
    }

    /// <summary>
    /// 擷取字串 By Byte
    /// </summary>
    /// <param name="str">要擷取的字串</param>
    /// <param name="length">要擷取的長度(字元)</param>
    /// <param name="replaceStr">超出後取代的文字</param>
    /// <returns>擷取後的字串</returns>
    public static string SubStringByBytes(string str, int length, string replaceStr)
    {
        ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        int allowByteLenth = length * 2;
        int stringByteLenth = GetStringBytes(str);

        //log.Debug("allowByteLenth = " + allowByteLenth);
        //log.Debug("stringByteLenth = " + stringByteLenth);
        
        string result = str;

        if (stringByteLenth > allowByteLenth)
        {
            byte[] bTemp = System.Text.Encoding.Default.GetBytes(str);
            result = Encoding.Default.GetString(bTemp, 0, allowByteLenth) + replaceStr;
        }

        return result;
    }
}